#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QAbstractButton>
#include <QTcpServer>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QNetworkInterface>
#include <QHostInfo>
#include <QtGlobal>
#include <QDateTime>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTimer>
#include <QTextCodec>
#include <QScreen>
#include <QScrollBar>
#include <QMouseEvent>

#include "settings.h"

#if defined (Q_OS_WINDOWS)
#define NEWLINE "\r\n"
#else
#define NEWLINE "\n"
#endif

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    int protocolType = 0;
    QTcpServer tcpServer;
    QTcpSocket tcpSocket;
    QUdpSocket udpSocket;
    quint32 recvByteCount = 0;
    quint32 sendByteCount = 0;
    bool isRunning = false;
    QString logFileName;
    QList<QTcpSocket*> connectedTcpClients;
    QTimer circleTimer;

private slots:
    void protocolTypeChannged(int index);
    void networkOperation();
    void newConnection();
    void readDataFromUdp();
    void readDataFromTcp();
    void sendOperation();
    void sendDataFormatChannged();
    void remoteHostDisconnected();

protected:
    void closeEvent(QCloseEvent* e) override;
    void showEvent(QShowEvent* e) override;
    bool eventFilter(QObject* obj, QEvent* event) override;

private:
    void showRecvText(const QString& time, const QString& address, const QByteArray& byteArray);
    uint8_t hex2chr(uint8_t hex);
    uint8_t chr2hex(uint8_t chr);
    void str2hex(const void* str, int len, uint8_t* hex);
    void signalsConnect();
    void controlsEnable(bool enabel);

    bool isIpAddress(const QString& ip);
};

#endif // MAINWINDOW_H
